#pragma once
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <sstream>  // 添加这一行
#include <thread>
#include <atomic>
#include <boost/asio.hpp>
#include <functional>
#include <algorithm>
#include <chrono>


class CAN {
public:
    CAN(const std::string& device, int baudrate = 1000000);
    ~CAN();
    void send(const can_frame& frame_);
    can_frame get_receive_frame();
    void set_Callback(std::function<void(const can_frame& frame)> slot);
    bool is_running(){return running_;};
private:
    std::string can_device;
    int socket_;
    ifreq ifr;
    sockaddr_can addr;
    std::atomic<can_frame> receive_frame;
    std::thread recv_thread;
    std::atomic<bool> running_;
    void receive();
    std::function<void(const can_frame& frame)> receive_callback;  // 修改这一行
};